package com.jiesen.base.modules.certificate.jsUser.controller;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.jiesen.base.modules.certificate.jsUser.model.JsUser;
import com.jiesen.base.modules.certificate.jsUser.model.JsUserDTO;
import com.jiesen.base.modules.certificate.jsUser.model.JsUserDetail;
import com.jiesen.base.modules.certificate.jsUser.service.IJsUserDetailService;
import com.jiesen.base.modules.certificate.jsUser.service.IJsUserService;
import com.jiesen.base.utils.JWTUtil;
import com.jiesen.base.utils.MD5Utils;
import com.jiesen.base.utils.R;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bao
 */

@Api(value = "测试controller")
@RestController
@RequestMapping(value = "/jsUser", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class JsUserController {

	@Autowired
	private IJsUserService jsUserService;

	@Autowired
	private IJsUserDetailService jsUserDetailService;

	@ApiOperation(value = "value", notes = "notes")
	@RequestMapping("/getList")
	@ResponseBody
	public R getList(JsUser jsUser) {
		return jsUserService.page(jsUser);
	}

	//学生登录
	@PostMapping("/login")
	public R JsUserLogin(JsUser jsUser){
		String telphone = jsUser.getTelphone();
		if(telphone==null){
			return R.error("请输入手机号");
		}
		JsUser user = jsUserService.findUserByTelphone(telphone);
		if(user==null){
			return R.error("用户不存在");
		}
		if(jsUser.getPassword()==null){
			jsUser.getCode().equals(user.getCode());
		}else {
			boolean b = MD5Utils.passwordCompare(jsUser.getPassword(), user.getPassword(), user.getSalt());
			if (!b) {
				return R.error("密码错误");
			}
		}
		return  R.ok().put("token", JWTUtil.createToken(user.getId()));
	}

	@GetMapping("/view")
	@ResponseBody
	public R findUserById(@RequestParam("id")String id){
		JsUser jsUser = jsUserService.getById(id);
		JsUserDetail userDetailByMainId = jsUserDetailService.findUserDetailByMainId(id);
		JsUserDTO jsUserDTO = new JsUserDTO(jsUser,userDetailByMainId);
		return R.ok().put("JsLoginUser",jsUserDTO);
	}

	@PostMapping("/creace")
	@ResponseBody
	public R creaceUser(JsUserDTO jsUserDTO){
		if(jsUserService.save(jsUserDTO)){
			return R.ok();
		}
		return R.error();
	}

	@PostMapping("/edit")
	@ResponseBody
	public R updateUser(JsUserDTO jsUserDTO){
		if(jsUserService.update(jsUserDTO)){
			return R.ok();
		}
		return R.error();
	}

	@GetMapping("/delete")
	@ResponseBody
	public R deleteUser(@RequestParam("id") String id) {
		if(jsUserService.delete(id)){
			return R.ok();
		}
		return R.error();
	}

	@GetMapping("/log")
	@ResponseBody
	public void log(@RequestParam("id") String id) {
		LogView view = new LogView();     
		final File tmpLogFile = new File("/home/apps/jars/server-dev/jiesen-server-dev.log"); 
		try {
			view.realtimeShowLog(tmpLogFile);
		} catch (IOException e) {
			e.printStackTrace();
		}  
	}

	public class LogView {     
		private long lastTimeFileSize = 0;  //上次文件大小     
		/**   
		 * 实时输出日志信息   
		 * @param logFile 日志文件   
		 * @throws IOException   
		 */    
		public void realtimeShowLog(File logFile) throws IOException{     
			//指定文件可读可写    
			final RandomAccessFile randomFile = new RandomAccessFile(logFile,"rw");
			//启动一个线程每1秒钟读取新增的日志信息     
			ScheduledExecutorService exec =      
					Executors.newScheduledThreadPool(1); 
			ScheduledFuture<?> withFixedDelay = exec.scheduleWithFixedDelay(new Runnable(){     
				public void run() {     
					try {     
						//获得变化部分的     
						randomFile.seek(lastTimeFileSize);     
						String tmp ="";
						tmp = randomFile.readLine();
						System.out.println("变化部分======"+new String(tmp.getBytes("iso-8859-1")));
						while(tmp !=null){
							tmp = randomFile.readLine();
						} 
						lastTimeFileSize = randomFile.length();     
					} catch (IOException e) {    
						throw new RuntimeException(e);     
					}     
				}     
			}, 0, 1, TimeUnit.SECONDS);     
		}     

	}
}